package com.dacd.filehelper;

import android.os.Environment;

import java.io.File;

public class FileUtils {

    /**
     * 获取SD卡路径
     *
     * @return
     */
    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        try {
            return sdDir.toString();
        } catch (NullPointerException e) {
        }
        return null;
    }


    /**
     * 删除所有文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {//如果该文件或者目录存在
            if (!file.isDirectory()) {
//                System.out.println("是文件");
                file.delete();
//                System.out.println("文件删除成功1111111111");
            } else if (file.isDirectory()) {
//                System.out.println("是文件夹！！！！！");
                String[] fileList = file.list();
                for (int i = 0; i < fileList.length; i++) {
                    File delFile = new File(filePath + "/" + fileList[i]);
                    if (!delFile.isDirectory()) {
                        System.out.println("path=" + delFile.getPath());
                        delFile.delete();
//                        System.out.println("删除文件成功");
                    } else if (delFile.isDirectory()) {
                        deleteFile(filePath + "/" + fileList[i]);
                    }
                }
                file.delete();
//                System.out.println("文件夹删除成功2222222222222！");
            }
        } else if (!file.exists()) {//如果该文件或者目录不存在
//            System.out.println("该文件或目录不存在！");
        }
        return true;
    }
}
